package org.csu.mypetstore.persistence;

import java.sql.*;

public class DBUtil {
    private static String driverString= "com.mysql.cj.jdbc.Driver";
    private static String connectionString= "jdbc:mysql://localhost:3306/mypetstore?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai";
    private static String username= "root";
    private static String password= "20021101";

    public static Connection getConnection() throws SQLException, ClassNotFoundException {
        Connection connection= null;
        try{
            Class.forName(driverString);
            connection= DriverManager.getConnection(connectionString, username, password);
        }catch (Exception e){
            throw e;
        }
        return connection;
    }
    public static void closeStatement(Statement statement) throws Exception{
        statement.close();
    }
    public static void closePreparedStatement(PreparedStatement statement) throws Exception{
        statement.close();
    }
    public static void closeResultSet(ResultSet resultSet) throws Exception{
        resultSet.close();
    }
    public static void closeConnection(Connection connection) throws Exception{
        connection.close();
    }

    public static void closePreparedStatent(PreparedStatement pStatement) throws Exception{
        pStatement.close();
    }

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Connection connection= DBUtil.getConnection();
        System.out.println(connection);
    }
}
